/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.news.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.news.domain.YxNews;
import co.yixiang.modules.news.domain.YxUserNews;
import co.yixiang.modules.news.service.YxNewsService;
import co.yixiang.modules.news.service.YxUserNewsService;
import co.yixiang.modules.news.service.dto.YxNewsDto;
import co.yixiang.modules.news.service.dto.YxNewsQueryCriteria;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* @author ycl
* @date 2020-08-13
*/
@AllArgsConstructor
@Api(tags = "消息管理")
@RestController
@RequestMapping("/api/yxNews")
public class YxNewsController {

    private final YxNewsService yxNewsService;
    private final YxUserNewsService yxUserNewsService;
    private final YxUserService yxUserService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxNews:list')")
    public void download(HttpServletResponse response, YxNewsQueryCriteria criteria) throws IOException {
        yxNewsService.download(generator.convert(yxNewsService.queryAll(criteria), YxNewsDto.class), response);
    }

    @GetMapping
    @Log("查询消息")
    @ApiOperation("查询消息")
    @PreAuthorize("@el.check('admin','yxNews:list')")
    public ResponseEntity<Object> getYxNewss(YxNewsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxNewsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增消息")
    @ApiOperation("新增消息")
    @PreAuthorize("@el.check('admin','yxNews:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxNews resources){

        yxNewsService.save(resources);
        
        //新增到用户信息表中
        List<YxUser> list = yxUserService.list(new QueryWrapper<YxUser>().lambda());
        list.forEach(yxUser -> {
            YxUserNews yxUserNews = new YxUserNews();
            yxUserNews.setUserId(yxUser.getUid());
            yxUserNews.setTitle(resources.getTitle());
            yxUserNews.setNewsId(resources.getId());
            yxUserNewsService.save(yxUserNews);
        });

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改消息")
    @ApiOperation("修改消息")
    @PreAuthorize("@el.check('admin','yxNews:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxNews resources){
        yxNewsService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除消息")
    @ApiOperation("删除消息")
    @PreAuthorize("@el.check('admin','yxNews:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            //删除信息
            yxNewsService.removeById(id);
            //删除用户信息
            yxUserNewsService.remove(new QueryWrapper<YxUserNews>().lambda().eq(YxUserNews::getNewsId , id));
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
